package com.mgkj.service.impl;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mgkj.common.result.Result;
import com.mgkj.common.utils.TimeUtils;
import com.mgkj.dto.yf.InitLldDto;
import com.mgkj.entity.Dscmb;
import com.mgkj.service.LogisticsRemoteService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LogisticsRemoteServiceImpl implements LogisticsRemoteService {


    private String logisticsUrl = "http://61.153.255.122:8421";

    private final RestTemplate restTemplate = new RestTemplate();


    @Override
    public Result<?> moInspection(String tf001, String tf002,String tf003) {
        // 拼接请求地址
        String url = logisticsUrl + "/api/paibao/moctgGenCPJ";

        // 构造请求参数对象
        JSONObject requestJson = new JSONObject();
        requestJson.set("tf001", tf001);
        requestJson.set("tf002", tf002);
        requestJson.set("tf003", tf003);

        // 发起 POST 请求
        HttpResponse response = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .body(requestJson.toString())
                .execute();

        // 响应处理
        if (response.isOk()) {
            try {
                JSONObject json = JSONUtil.parseObj(response.body());

                Integer code = json.getInt("code");
                String message = json.getStr("message");
                Object data = json.get("data");

                if (code != null && code == 200) {
                    // 成功返回
                    return Result.ok(data);
                } else {
                    // 接口业务逻辑错误
                    return Result.fail(message);
                }
            } catch (Exception e) {
                return Result.fail("返回解析失败：" + e.getMessage());
            }
        } else {
            return Result.fail("接口请求失败，状态码：" + response.getStatus());
        }
    }



}
